package com.prs.crm.action.hr;

import javax.smartcardio.ATR;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.hr.Attribute;
import com.prs.crm.service.hr.AttributeService;

public class AttributeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Attribute attribute;
	private AttributeService attributeService;
	private PaginationSupport pagination;
	private Integer[] attributeIds;

	@Action("listAttribute")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Attribute.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getAttributeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editAttribute")
	public String edit() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveAttribute", results = {
			@Result(name = "success", type = "redirect", location = "listAttribute"),
			@Result(name = "error", type = "chain", location = "editAttribute") })
	public String save() {
		if (attribute.getType() == null) {
			addActionError("类型不能为空");
			return ERROR;
		}
		if (attribute.getValue() == null
				|| "".equals(attribute.getValue().trim())) {
			addActionError("类型值不能为空");
			return ERROR;
		}
		this.getAttributeService().save(attribute);
		return SUCCESS;
	}

	@Action(value = "removeAttribute", results = { @Result(name = "success", type = "redirect", location = "listAttribute") })
	public String remove() {
		this.getAttributeService().batchRemove(this.getAttributeIds());
		return SUCCESS;
	}

	public Attribute getAttribute() {
		return attribute;
	}

	public void setAttribute(Attribute attribute) {
		this.attribute = attribute;
	}

	public AttributeService getAttributeService() {
		return attributeService;
	}

	public void setAttributeService(AttributeService attributeService) {
		this.attributeService = attributeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getAttributeIds() {
		return attributeIds;
	}

	public void setAttributeIds(Integer[] attributeIds) {
		this.attributeIds = attributeIds;
	}

}
